package com.example.ruiji.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ruiji.common.R;
import com.example.ruiji.entity.Category;
import com.example.ruiji.service.CategroyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategroyController {
    @Autowired
    private CategroyService categroyService;

    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category:{}",category);
        categroyService.save(category);
        return R.success("新增分类成功");

    }
    //菜品分类
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        //分页构造器
        Page<Category> page1 = new Page<>();
        //条件构造器对象
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序
        queryWrapper.orderByAsc(Category::getSort);
        categroyService.page(page1,queryWrapper);
        return R.success(page1);
    }
    //根据id删除分类
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除分类：id为{}",ids);
        categroyService.remove(ids);
        return R.success("分类信息删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类信息：{}",category);
        categroyService.updateById(category);
        return R.success("修改分类信息成功");
    }
    //菜品分类下拉框
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categroyService.list(queryWrapper);
        return  R.success(list);
    }

}
